'use strict';

const program = require('commander');
//定义参数,以及参数内容的描述
program.option('-f, --config <path>','配置文件').parse(process.argv);
const configPath=program["config"] || './config';
const config=require(configPath);
const log4js=require("log4js");
const logger=log4js.getLogger();
log4js.configure(config.log4js);

if (config.monitor) {
    const easyMonitor = require('easy-monitor');
    easyMonitor(config.monitor);
}

const Thenjs=require('thenjs');
const _=require('lodash');
const nconf = require('nconf');
let path=require('path');

global.Thenjs=Thenjs;
global._=require('lodash');
global.config=config;

if (config.redis) {
    require('nconf-redis')
    nconf.use('redis', config.redis);
}

global.common=require('./common');

let uncaughtExceptionCount=0;
function uncaughtException(err) {
    uncaughtExceptionCount++;
    logger.error(err.stack)
    Thenjs((cont) => {
        if (uncaughtExceptionCount > 100) {
            logger.error('未捕捉异常太多程序退出'+ err.stack);
            process.exit(1);
        }
    }).fail((c, err) => {
        logger.error('error when exit'+ err.stack);
    });
}

process.on('uncaughtException', uncaughtException);
Thenjs.onerror=uncaughtException;

let app = require('./app');